//
//  ReplyV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/29.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

enum ReplyVStyle {
  
  case onlyReply
  case other
  
}

class ReplyV: UIView {
  
  var replyClosure: VoidClosure?
  
  var sendOnClosure: VoidClosure?
  
  var finishClosure: VoidClosure?
  
  var style: ReplyVStyle! {
    
    didSet {
      create()
    }
    
  }
  
  fileprivate var sendOnButton: UIButton!
  fileprivate var finishButton: UIButton!
  fileprivate var replyButton: UIButton!

  override init(frame: CGRect) {
    super.init(frame: frame)
    
    backgroundColor = UIColor.white
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - action

extension ReplyV {
  
  @objc fileprivate func relpyButtonAction() {
    
    guard let closure = replyClosure else { return }
    closure()
    
  }
  
  @objc fileprivate func sendOnAction() {
    guard let closure = sendOnClosure else { return }
    closure()
  }
  
  @objc fileprivate func finishAction() {
    guard let closure = finishClosure else { return }
    closure()
  }
}

// MARK: - fileprivate

extension ReplyV {
  fileprivate func create() {
    
    
    replyButton = UIButton()
    addSubview(replyButton)
    replyButton.backgroundColor = GlobalConfig.BGWhiteColor
    replyButton.titleLabel?.font = GlobalConfig.system16Font
    replyButton.setTitle("回复", for: .normal)
    replyButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
    replyButton.layer.cornerRadius = 4
    replyButton.clipsToBounds = true
    replyButton.contentHorizontalAlignment = .left
    replyButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
    replyButton.addTarget(self, action: #selector(relpyButtonAction), for: .touchUpInside)
    
    
    if .onlyReply == style {
      
      replyButton.snp.makeConstraints { (make) in
        make.left.equalTo(15)
        make.centerY.equalToSuperview()
        make.height.equalTo(33)
        make.right.equalTo(-15)
      }
      
    }
    else if .other == style {
      
      replyButton.snp.makeConstraints { (make) in
        make.left.equalTo(15)
        make.centerY.equalToSuperview()
        make.height.equalTo(33)
        make.width.equalTo((UIScreen.width - 15) / 2)
      }
      
      sendOnButton = UIButton()
      addSubview(sendOnButton)
      sendOnButton.setTitle("转交", for: .normal)
      sendOnButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
      sendOnButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
      let sendImage = UIImage(named: "message-tab-transfer-icon")
      sendOnButton.setImage(sendImage, for: .normal)
      sendOnButton.addTarget(self, action: #selector(sendOnAction), for: .touchUpInside)
      sendOnButton.snp.makeConstraints { (make) in
        make.left.equalTo(replyButton.snp.right)
        make.width.equalTo((UIScreen.width - 15) / 4)
        make.height.equalTo(33)
        make.centerY.equalToSuperview()
      }
      sendOnButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
      
      finishButton = UIButton()
      addSubview(finishButton)
      finishButton.setTitle("完结", for: .normal)
      finishButton.setTitleColor(GlobalConfig.TextLightColor, for: .normal)
      finishButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
      let finishImage = UIImage(named: "message-tab-end-icon")
      finishButton.setImage(finishImage, for: .normal)
      finishButton.addTarget(self, action: #selector(finishAction), for: .touchUpInside)
      finishButton.snp.makeConstraints { (make) in
        make.left.equalTo(sendOnButton.snp.right)
        make.width.equalTo((UIScreen.width - 15) / 4)
        make.height.equalTo(33)
        make.centerY.equalToSuperview()
      }
      finishButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
    }
    
    
  }
}
